package com.jiutong.meipengdai.ui.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jiutong.meipengdai.R;
import com.jiutong.meipengdai.base.BaseActivity;
import com.jiutong.meipengdai.entity.IdentifyCodeRespond;
import com.jiutong.meipengdai.http.model.interfaces.RegisterAndLoginBiz;
import com.jiutong.meipengdai.http.present.UserInfoPresent;
import com.jiutong.meipengdai.utils.StringUtil;
import com.jiutong.meipengdai.utils.TimerUtil;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by suxi on 2017/6/2.
 */

public class NewPhoneBinddingActivity extends BaseActivity implements RegisterAndLoginBiz {

    @Bind(R.id.rl_back)
    RelativeLayout rl_back;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_old_phone)
    TextView tv_old_phone;
    @Bind(R.id.et_identify_code_old)
    EditText et_identify_code_old;
    @Bind(R.id.tv_getcode_old)
    TextView tv_getcode_old;
    @Bind(R.id.btn_phone_next)
    Button btn_phone_next;

    private UserInfoPresent userInfoPresent;

    private TimerUtil timerUtil;

    private String identifyCode;

    //正在获取验证码
    private boolean isVrfCodeGetting;

    @Override
    public int getLayoutResouceId() {
        return R.layout.activity_phone_bindding;
    }

    @Override
    protected void initDate(Bundle savedInstanceState) {
        super.initDate(savedInstanceState);
    }

    @Override
    protected void initView() {
        super.initView();
        ButterKnife.bind(this);
        tvTitle.setText(R.string.change_phone);

        Intent intent = getIntent();
        tv_old_phone.setText(intent.getExtras().getString("phone"));
        //设置填写正确的状态
        btn_phone_next.setEnabled(false);
        et_identify_code_old.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (getVerificatCode().length() != 6) {
                    btn_phone_next.setBackgroundResource(R.drawable.shape_light_gray_btn);
                    btn_phone_next.setEnabled(false);
                } else {
                    btn_phone_next.setBackgroundResource(R.drawable.selector_orange_btn_bg);
                    btn_phone_next.setEnabled(true);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        timerUtil = new TimerUtil(tv_getcode_old);
        timerUtil.setCountTimer();


        userInfoPresent = new UserInfoPresent(this, this);

    }

    @OnClick({R.id.rl_back, R.id.tv_getcode_old, R.id.btn_phone_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            case R.id.tv_getcode_old:  //旧手机验证码
                userInfoPresent.getAuthCode();
                break;
            case R.id.btn_phone_next:  //下一步
                if (isRightAction())
                    openActivity(EditPhoneActivity.class);
                break;
        }
    }

    /**
     * 操作是否正确
     *
     * @return
     */
    private boolean isRightAction() {
        if (!StringUtil.isMobileNumber(getPhoneNum())) {
            toastShortShow("手机号码格式有误，请输入正确手机号码");
            return false;
        }

        if (TextUtils.isEmpty(getVerificatCode())) {
            toastShortShow("请输入手机验证码");
            return false;
        }
        if (!identifyCode.equals(getVerificatCode())) {
            toastShortShow("您输入的验证码不正确");
            return false;
        }
        return true;
    }

    @Override
    public String getPhoneNum() {
        return tv_old_phone.getText().toString().trim();
    }

    @Override
    public String getPassword() {
        return null;
    }

    @Override
    public String getVerificatCode() {
        return et_identify_code_old.getText().toString().trim();
    }

    @Override
    public String getOriPwd() {
        return null;
    }

    @Override
    public String getNewPwd() {
        return null;
    }

    @Override
    public String getRelName() {
        return null;
    }

    @Override
    public String getIdCard() {
        return null;
    }

    @Override
    public String getEmail() {
        return null;
    }

    @Override
    public String getNewPhone() {
        return null;
    }

    @Override
    public String getCardNumber() {
        return null;
    }

    @Override
    public void onVerCodeGetSuccess(IdentifyCodeRespond subjects) {
        if (subjects.getResCode().equals("200")) {
            identifyCode = subjects.getData();
            timerUtil.startTimer();
        }else{
            toastShortShow(subjects.getResDesc());
        }
    }

    @Override
    public void onSuccess(Object object) {

    }

    @Override
    public void onError(Throwable e) {

    }
}
